/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.com.util;

/**
 *
 * @author romulogalindo
 */
import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;

public class FindAfile implements FileVisitor {

//    private Path searchThisFile;
    private String searchThisFileName;
    private String f;
//    private List<String>  listFiles=new ArrayList<>();
    private List<File>  listFiles=new ArrayList<>();

    public FindAfile() {
        searchThisFileName = "";
    }

    public FindAfile(String searchFileName) {
        this.searchThisFileName = searchFileName;
    }

    @Override
    public FileVisitResult preVisitDirectory(Object dir, BasicFileAttributes attrs) throws IOException {
//        System.out.println("directory to be visited next " + (Path) dir);
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult visitFile(Object file, BasicFileAttributes attrs) throws IOException {
        search((Path) file);
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult visitFileFailed(Object file, IOException exc) throws IOException {
//        System.out.println((Path) file);
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult postVisitDirectory(Object dir, IOException exc) throws IOException {
//        System.out.println("finished with " + (Path) dir);
        return FileVisitResult.CONTINUE;
    }

    public boolean search(Path file) throws IOException {
        Path name = file.getFileName();

//        System.out.println(name.toString().indexOf(searchThisFileName) + ">" + -1 + "&(" + name.toString().indexOf(searchThisFileName) + "+ 4 == " + name.toString().length() + ")");
        if (name.toString().indexOf(searchThisFileName) > -1 & (name.toString().indexOf(searchThisFileName) + 4 == name.toString().length())) {
//            listFiles.add(file.toRealPath().toString());
            listFiles.add(file.toFile());
//            System.out.println("name::" + file.toRealPath().toString());
        }

//        if (name != null && (name.equals(searchThisFileName) | name.toString().substring(0, name.toString().length() - 4).contentEquals(searchThisFileName))) {
//            f = file.toRealPath().toString();
//            return true;
//        } else {
//            return false;
//        }

        return false;
    }

    public void searchThis(String searchedFileName) {
        this.searchThisFileName = searchedFileName;
        this.f = null;
    }

    public String getFile() {
        return f;
    }

//    public List<String> getListFiles() {
//        return listFiles;
//    }
    public List<File> getListFiles() {
        return listFiles;
    }
    

}
